package com.example.accountbook;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class AccountAllFragment extends Fragment {//继承碎片类

    private RecyclerView rvAccount;//声明循环控件
    private AccountItemAdapter adapter;//声明自定义列表项适配器对象
    private List<CostBean>list;//声明一个List

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.account_list,container,false);//为碎片加载布局
        rvAccount = view.findViewById(R.id.rv_account);//获取循环控件实列

        initData();//调用方法生成数据

        adapter = new AccountItemAdapter(this.getContext(),list);//创建列表项适配器对象，传入当前类Context和list
        LinearLayoutManager layout = new LinearLayoutManager(this.getContext());//创建线性布局管理对象，传入当前类Context

        rvAccount.setLayoutManager(layout);//设置布局管理器
        rvAccount.setAdapter(adapter);//设置适配器
        rvAccount.setItemAnimator(new DefaultItemAnimator());//设置Item增加，移除动画
        rvAccount.addItemDecoration(new DividerItemDecoration(this.getContext(),DividerItemDecoration.VERTICAL));//添加分割线

        return view;//返回view
    }

    public void initData() {//自定义方法，用于创建数据类对象并存入list

        list = new ArrayList<>();//创建列表

        for (int i = 0; i < 5; i++) {
            CostBean bean = new CostBean();//创建数据类对象
            bean.setType("全部");//调用数据类方法存入数据
            bean.setTitle("周末晚餐");
            bean.setExplain("周末同学聚会");
            bean.setTime("2018-11-26");
            bean.setMoney(50);
            list.add(bean);//将数据类添加到list
        }
    }
}
